package com.infolab.aims.service;

import java.util.Date;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infolab.aims.common.PaginationHelper;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.Section;
import com.infolab.aims.service.intf.DepartmentService;


@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
	
	Log logger = LogFactory.getLog(getClass());
	@PersistenceContext
	private EntityManager em;
	
	public void createDepartment(Department department)
	{
		department.setActive(true); 
		
		Department newDepartment = new Department();
		
		department.setModifiedBy(department.getCreatedBy());
		department.setDateCreated(new Date());
		department.setDateModified(new Date());

		BeanUtils.copyProperties(department,newDepartment);
		
		em.persist(newDepartment);
		
		department.setDepartmentId(newDepartment.getDepartmentId());
		
	}

	public void updateDepartment(Department department) {
		
		Department newDepartment = em.find(Department.class, department.getDepartmentId());

		department.setDateModified(new Date());
		BeanUtils.copyProperties(department,newDepartment);
		
		em.merge(newDepartment);
	}


	public Department getDepartment(Integer departmentId) {
		
		return em.find(Department.class, departmentId);
	}

	public Department findDepartment(SearchCriteria searchCriteria) {
		
		return null;
	}
	
	public void deleteDepartment(Integer departmentId) {
		
		Query query = em.createQuery("update Department set active=0 where departmentId=:departmentId");
		query.setParameter("departmentId", departmentId);
		query.executeUpdate();
		
	}
	
	public void deleteDepartment(Department department) {
		department.setDateModified(new Date());
		department.setActive(false);
		em.merge(department);
	}	

	public SearchResult searchDepartment(
			SearchCriteria searchCriteria) {
		
		if(searchCriteria.getCurPage()==null)
			searchCriteria.setCurPage(1);
		
		DepartmentPaginationHelper helper = new DepartmentPaginationHelper();
		
		return helper.getSearchResult(em, searchCriteria);
	}
	
	public static class DepartmentPaginationHelper extends PaginationHelper<Department> {

		@Override
		public String[] getValidColumnName() {
			return new String[]{"departmentCode", "departmentName", "departmentUnitNo", "departmentContact", "active"};
		}
		
		@Override
		public String[] getValidSortName() {
			return new String[]{"departmentCode","departmentName","departmentUnitNo","departmentContact", "modifiedBy", "dateModified"};
		}

		@Override
		public String getDefaultSortName() {
			return "departmentName";
		}
	}
}
